package com.j2110.controller;

import com.j2110.pojo.Emp;
import com.j2110.service.EmpService;
import com.j2110.utils.Result;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import org.apache.commons.beanutils.BeanUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/emp/*")
public class EmpServlet extends BaseServlet {

    public void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        EmpService service = new EmpService();
        List<Emp> list = service.getEmps();
        req.setAttribute("EMP_LIST", list);
        req.getRequestDispatcher("/WEB-INF/pages/emp.jsp").forward(req, resp);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户提交数据
        Map<String, String[]> map = req.getParameterMap();
        String sex = req.getParameter("sex");
        //将数据封装到对象中
        Emp stu = new Emp();
        try {
            BeanUtils.populate(stu, map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (sex.equals("m")){
            stu.setSex(true);
        }
        EmpService service = new EmpService();
        Result result = service.execute(stu);
        if (result.isSuccess()){
            resp.sendRedirect(req.getContextPath() + "/emp/index");
        }else {
            resp.sendRedirect(req.getContextPath() + "/emp.jsp");
        }
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        EmpService service = new EmpService();
        Result re = service.delete(id);
        if (re.isSuccess()){
            resp.sendRedirect(req.getContextPath() + "/emp/index");
        }
    }

    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        EmpService service = new EmpService();
        Result result = service.query(id);
        System.out.println(id);
        System.out.println(result.isSuccess());
        if (result.isSuccess()){
            req.setAttribute("list", result.getData());
            req.getRequestDispatcher("/WEB-INF/pages/emp_edit.jsp").forward(req, resp);
        }else {
            resp.sendRedirect(req.getContextPath() + "/emp/index");
        }
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取提交数据
        Map<String, String[]> map = req.getParameterMap();
        //封装数据
        Emp stu = new Emp();
        try {
            BeanUtils.populate(stu,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EmpService service = new EmpService();
        Result re = service.update(stu);
        if (re.isSuccess()){
            req.getRequestDispatcher("emp/index").forward(req, resp);
        }else {
            resp.sendRedirect(req.getContextPath() + "emp.jsp");
        }
    }

    public void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String search = req.getParameter("search");
        EmpService service = new EmpService();
        Result re = service.query(search);
        Result result = service.queryByName(search);
        if (re.isSuccess()){
            req.setAttribute("EMP_LIST", re.getData());
            req.getRequestDispatcher("/WEB-INF/pages/emp.jsp").forward(req, resp);
        }else if (result.isSuccess()){
            req.setAttribute("EMP_LIST", result.getData());
            req.getRequestDispatcher("/WEB-INF/pages/emp.jsp").forward(req, resp);
        } else {
            resp.sendRedirect(req.getContextPath() + "/emp/index");
        }
    }
}
